from flask_restful import Resource, fields, marshal_with, request, output_json, marshal
import service.back_ground as work_order_service
import view.work_order as view_wo
import view.common as view_common
from utils.common.jwt_middleware import jwt_required, get_jwt_identity, all_jwt_required, get_jwt
from exception.api_exception import APIException
from enums.work_order_type_enum import WorkOrderTypeEnum


class GetDetails(Resource):
    """
    查询工单列表
    :param: page_num -> 页数,               :type: int
    :param: page_size -> 单页条数,           :type: int
    :param: status -> 工单状态,              :type: string
    :param: customer_name -> 客户淘宝名称     :type: array
    :param: order_key -> 排序字段            :type: array
    :param: show_self -> 只看自己            :type: bool
    :return: json统一返回格式
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @all_jwt_required()
    @marshal_with(res_obj)
    def get(self):
        # data = request.get_json()
        data = request.args
        if not data:
            raise APIException(message="参数为空")
        try:
            page_num = int(data.get('page_num')) if int(data.get('page_num')) > 0 else 0
            page_size = int(data.get('page_size')) if int(data.get('page_size')) > 0 else 0
            status = int(data.get('status')) if data.get('status') else -1
            customer_name = data.get('customer_name')
            order_id = data.get('order_id')
            oper_type = data.get('oper_type')
            order_key = data.get('order_key')
            show_self = bool(data.get('show_self'))
            shop_id = int(data.get('shop_id')) if data.get('shop_id') else -1
            make_user_id = int(data.get('make_user_id')) if data.get('make_user_id') else None
            get_user_id = int(data.get('get_user_id')) if data.get('get_user_id') else None
            link = data.get('link') if data.get('link') else None
            work_order_type = data.get('work_order_type') or WorkOrderTypeEnum.ALL.value

            current_user = get_jwt_identity()
            cur_user_id = current_user['id']
        except Exception as e:
            raise APIException(message="参数解析失败")
        v = view_wo.Details(page_num=page_num, page_size=page_size, status=status, customer_name=customer_name,
                            order_key=order_key, show_self=show_self, shop_id=shop_id, order_id=order_id,
                            oper_type=oper_type, make_user_id=make_user_id, link=link, get_user_id=get_user_id,
                            work_order_type=work_order_type)
        res = work_order_service.details(v, cur_user_id)
        return view_common.success(res=res)


class DocumentMakingBillboard(Resource):
    submit_res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @all_jwt_required()
    @marshal_with(submit_res_obj)
    def get(self):
        current_user = get_jwt_identity()
        user_id = current_user['id']
        res = work_order_service.document_making_billboard(user_id)
        return view_common.success(res=res)


class Dashboard(Resource):
    submit_res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    # @all_jwt_required()
    @marshal_with(submit_res_obj)
    def get(self):
        # current_user = get_jwt_identity()
        # user_id = current_user['id']
        res = work_order_service.dashboard(request.args)
        return view_common.success(res=res)


class CreateVirtualOrder(Resource):
    submit_res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @all_jwt_required()
    @marshal_with(submit_res_obj)
    def post(self):
        current_user = get_jwt_identity()
        user_id = current_user['id']
        data = request.get_json()
        link = data.get('link')
        size = data.get('size')
        note = data.get('note')
        work_order_type = data.get('work_order_type')
        res = work_order_service.create_virtual_order(link, size, user_id, note, work_order_type)
        return view_common.success(res=res)


class Chargeback(Resource):
    """
    退款
    """
    submit_res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @all_jwt_required()
    @marshal_with(submit_res_obj)
    def post(self):
        data = request.get_json()
        work_order_id = data.get('work_order_id')
        refund_amount = data.get('refund_amount')  # 退款金额
        current_user = get_jwt_identity()
        cur_user_id = current_user['id']
        res = work_order_service.chargeback(cur_user_id, work_order_id, refund_amount)
        return view_common.success(res=res)


class CreateOrder(Resource):
    submit_res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @all_jwt_required()
    @marshal_with(submit_res_obj)
    def post(self):
        data = request.get_json()
        current_user = get_jwt_identity()
        cur_user_id = current_user['id']
        res = work_order_service.create_order(data, cur_user_id)
        return view_common.success(res=res)


class UserAuth(Resource):
    submit_res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @all_jwt_required()
    @marshal_with(submit_res_obj)
    def get(self):
        current_user = get_jwt_identity()
        cur_user_id = current_user['id']
        if not cur_user_id:
            raise APIException(message="查询用户id为空")
        res = work_order_service.user_auth(cur_user_id)
        return view_common.success(res=res)


class Submit(Resource):
    datas = {
        'id': fields.Integer,
        'ww': fields.String,
        'order_id': fields.String,
        'shop_id': fields.Integer,
        'make_user_id': fields.Integer,
        'get_user_id': fields.Integer,
        'cost': fields.String,
        'total_size': fields.Integer,
    }
    res = {
        'total': fields.Integer,
        'datas': fields.List(fields.Nested(datas))
    }
    submit_res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.List(fields.Nested(res))
    }

    @all_jwt_required()
    @marshal_with(submit_res_obj)
    def post(self):
        res = work_order_service.submit()
        return view_common.success(res=res)


class GetUsersMakeGet(Resource):
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @all_jwt_required()
    @marshal_with(res_obj)
    def get(self):
        current_user = get_jwt_identity()
        cur_user_id = current_user['id']
        res = work_order_service.get_users_make_get(cur_user_id)
        return view_common.success(res=res)


class GetUsers(Resource):
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @all_jwt_required()
    @marshal_with(res_obj)
    def get(self):
        res = work_order_service.get_users()
        return view_common.success(res=res)


class GetUsingShops(Resource):
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @all_jwt_required()
    @marshal_with(res_obj)
    def get(self):
        res = work_order_service.get_using_shops()
        return view_common.success(res=res)


class DeleteShop(Resource):
    """
    删除店铺
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @all_jwt_required()
    @marshal_with(res_obj)
    def get(self):
        data = request.args
        if data is None:
            return view_common.failed_response(res="参数为空")
        shop_id_str = data.get('shop_id')
        if shop_id_str is None:
            return view_common.failed_response(res="店铺id为空")
        shop_id = int(shop_id_str)
        res = work_order_service.delete_shop(shop_id)
        return view_common.success(res=res)


class CreateShop(Resource):
    """
    创建商店
    """
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.Raw
    }

    @all_jwt_required()
    @marshal_with(res_obj)
    def post(self):
        data = request.get_json()
        if data is None:
            return view_common.failed_response(res="参数为空")
        shop_name = data.get('shop_name')
        code_str = data.get('code')
        if code_str is None:
            return view_common.failed_response(res="code为空")
        code = int(code_str)
        res = work_order_service.create_shop(code, shop_name)
        return view_common.success(res=res)


class GetShops(Resource):
    """
    查询所有门店信息
    """
    datas = {
        'id': fields.Integer,
        'code': fields.String,
        'shop_name': fields.String
    }
    res = {
        'total': fields.Integer,
        'datas': fields.List(fields.Nested(datas))
    }
    res_obj = {
        'code': fields.Integer,
        'message': fields.String,
        'result': fields.List(fields.Nested(res))
    }

    @all_jwt_required()
    @marshal_with(res_obj)
    def get(self):
        data = request.args
        if data is None:
            return view_common.failed_response(res="参数为空")
        page_num_str = data.get('page_num')
        page_num, page_size = 0, 10
        if page_num_str is not None:
            page_num = int(page_num_str)
            page_num = page_num if page_num > 0 else 0
        page_size_str = data.get('page_size')
        if page_size_str is not None:
            page_size = int(page_size_str)
            page_size = page_size if page_size > 0 else 10
        res = work_order_service.get_shops(page_num, page_size)
        return view_common.success(res=res)
